<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Category;
use App\Models\Child;
use App\Models\CurrentChild;
use App\Models\Device;
use App\Models\Game;
use App\Models\Grade;
use App\Models\History;
use App\Models\User;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UserController extends BaseController
{
    public function index(Request $request)
    {

        $user = $this->getUser();
        $page_num = config("site.other.paginate");
        $get_page_num = $request->input("paginate");
        if (isset($get_page_num) && is_numeric($get_page_num) && $get_page_num > 0){
            $page_num = $get_page_num;
        }

        $children = $user->children()
            ->limit($page_num)
            ->orderBy("current", "desc")
            ->orderBy("last_use_time", "desc")
            ->orderBy("id", "desc")
            ->get();
        $grades = Grade::getArr();
        $catgories = Category::getArr();

        $children->each(function (&$item) use ($grades, $catgories){
            $item->history = $this->getHistory($item->id, $grades, $catgories);
        });

        $page_count = floor($user->children()->count() / $page_num);
        $where = ['user_id' => $user->id];
        $device_code = $request->input('device_code');

        if (!empty($device_code)) {
            $device = Device::where('code', $device_code)->first();
            if (empty($device)) {
                return $this->json([], config("apicode.deviceFail.msg"), config("apicode.deviceFail.code"));
            }
            $where['device_code'] = $device_code;
        }

        $currentChild = CurrentChild::where($where)->orderBy('id', 'desc')->value('child_id');

        empty($currentChild) && $currentChild = Child::where("user_id", $user->id)->orderBy('id', 'desc')->value("id");

        empty($currentChild) && $currentChild = 0;

        $currentChildHistory = $this->getHistory($currentChild, $grades, $catgories);

        $userinfo = $user->toArray();
        $children = $children->toArray();

        return $this->json(compact('userinfo', 'children', 'page_count','currentChild', 'currentChildHistory'));
    }

    private function getHistory($child_id, $grades=[], $catgories=[]){

        $history = History::where("child_id",$child_id)
            ->select("grade_id", "category_id", "game_id", 'end_star')
            ->orderBy("id", "desc")->first();

        if (!empty($history)) {
            $history->grade = !empty($history->grade_id) ? $grades[$history->grade_id] : 0;
            $history->category = !empty($history->category_id) ? $catgories[$history->category_id] : 0;
            $game = Game::select('code','name')->find($history->game_id);
            $history->game =$game->name;
            $history->game_code =$game->code;
        }else{
            //Log::error("empty");
            $data = ['grade_id'=>1,
                'category_id'=>1,
                'game_id'=>0,
                'end_start'=>0,
                'game'=>'',
                'game_code'=>'',
                ];
            $data['grade'] = $grades[$data["grade_id"]];
            $data['category'] = $catgories[$data["category_id"]];

            $history = collect($data);

            //Log::error($history);
        }

        return $history;
    }
}
